package com.sjq.project.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import org.springframework.jdbc.support.rowset.SqlRowSet;
import com.sjq.project.dao.ProjectFileDao;
import com.sjq.common.dao.GenericDao;
import com.sjq.common.util.PageResults;
import com.sjq.project.entity.ProjectFile;

@Repository
public class ProjectFileDaoImpl extends GenericDao<ProjectFile> implements ProjectFileDao {

	 @Override
	 public PageResults<?> selectFileByPage(int pageNo, int pageSize,ProjectFile file){
	 	StringBuilder paramSql = new StringBuilder();
		List params = new ArrayList();
		//查询条件
		/*if (file.getXxzjbh() != null&& !(file.getXxzjbh().equals(""))) {
			paramSql.append(" and xxzjbh=? ");
			params.add(file.getXxzjbh().trim());
		}*/
		String sql_fenye="select WJLX,XXZJBH,GLBH_XXZJBH,LCDLDM,LCXLDM,WJLBDM,SXH,WJCCLJ,ISCONVERSION,DJBGBS,to_char(DJBGSJ,'yyyy-MM-dd hh24:mi:ss') as DJBGSJ,WED_SXH,WED_MC,GLWJ_XXZJBH from T_XM_FILE where DJBGBS <>3 " + paramSql.toString() ;
		PageResults<Map<String, String>> results = new PageResults<Map<String, String>>();
		StringBuilder pageSql = new StringBuilder()
				.append("select * from ")
				.append(" (select tmp_1.*, rownum as rowindex from ("+sql_fenye+") tmp_1 ) tmp_2 ")
				.append(" where rowindex between ")
				.append(((pageNo-1)*pageSize + 1))
				.append(" and ")
				.append(((pageNo - 1) * pageSize + pageSize));
		List<Map<String, String>> list=new ArrayList<Map<String, String>>();
		try {
			list = this.getListBySql(pageSql.toString(),params.toArray());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String sql="select count(1) as num from T_XM_FILE where DJBGBS <>3  "+paramSql.toString()+" ";
		SqlRowSet sqlRowSet = this.getJdbcTemplate().queryForRowSet(sql,params.toArray());
		int count = 0;
		while(sqlRowSet.next()){
			count = sqlRowSet.getInt("num");
		}
		results.setTotalCount(count);
        results.setPageNo(pageNo);
        results.setPageSize(pageSize);
		results.setResults(list);
		return results;	
	 }
	 @Override
	 public int addFile(ProjectFile file){
	 	String sql="insert into T_XM_FILE ( wjlx,xxzjbh,glbh_xxzjbh,lcdldm,lcxldm,wjlbdm,sxh,wjcclj,isconversion,djbgbs,djbgsj,wed_sxh,wed_mc,glwj_xxzjbh) values (?,?,?,?,?,?,?,?,?,?,to_date(?,'yyyy-MM-dd hh24:mi:ss'),?,?,?)  ";
	 	Object [] params={file.getWjlx(),file.getXxzjbh(),file.getGlbh_xxzjbh(),file.getLcdldm(),file.getLcxldm(),file.getWjlbdm(),file.getSxh(),file.getWjcclj(),file.getIsconversion(),file.getDjbgbs(),file.getDjbgsj(),file.getWed_sxh(),file.getWed_mc(),file.getGlwj_xxzjbh()};
	 	return this.getJdbcTemplate().update(sql.toString(),params);
	 }
	 
	 @Override
	 public int deleteFile(String fileId){
		 String sql="update T_XM_FILE set djbgbs=3 where xxzjbh=?";
		 Object [] params={ fileId};
		 return this.getJdbcTemplate().update(sql.toString(),params);
	 }
	 
	  @Override
	 public int updateFile(ProjectFile file){
	 	String sql="update T_XM_FILE set WJLX=?,XXZJBH=?,GLBH_XXZJBH=?,LCDLDM=?,LCXLDM=?,WJLBDM=?,SXH=?,WJCCLJ=?,ISCONVERSION=?,DJBGBS=?,DJBGSJ=?,WED_SXH=?,WED_MC=?,GLWJ_XXZJBH=?  where xxzjbh=?";
	 	Object [] params={file.getWjlx(),file.getXxzjbh(),file.getGlbh_xxzjbh(),file.getLcdldm(),file.getLcxldm(),file.getWjlbdm(),file.getSxh(),file.getWjcclj(),file.getIsconversion(),file.getDjbgbs(),file.getDjbgsj(),file.getWed_sxh(),file.getWed_mc(),file.getGlwj_xxzjbh()};
	 	return this.getJdbcTemplate().update(sql.toString(),params);
	 }
	 
	 @Override
	 public List<Map<String, String>> findFileByListAll(ProjectFile file){
		StringBuilder paramSql = new StringBuilder();
		List params = new ArrayList();
		//查询条件
		if (file.getXxzjbh() != null&& !(file.getXxzjbh().equals(""))) {
			paramSql.append(" and xxzjbh=? ");
			params.add(file.getXxzjbh().trim());
		}
		String sql="select WJLX,XXZJBH,GLBH_XXZJBH,LCDLDM,LCXLDM,WJLBDM,SXH,WJCCLJ,ISCONVERSION,DJBGBS,to_char(DJBGSJ,'yyyy-MM-dd hh24:mi:ss') as DJBGSJ,WED_SXH,WED_MC,GLWJ_XXZJBH from T_XM_FILE where DJBGBS <>3  "+ paramSql.toString() ;
		List<Map<String, String>> list=new ArrayList<Map<String, String>>();
		try {
			list = this.getListBySql(sql.toString(),params.toArray());
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}
}